package com.cskaoyan.shopping.service.impl.yyr;

import com.cskaoyan.mall.commons.exception.yyr.ExceptionProcessorUtils;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dto.yyr.NavListResponse;
import com.cskaoyan.shopping.service.yyr.IContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description：
 * @Author: 杨涌仁
 * @Date: 2022/08/25/0025 11:13
 */
@Slf4j
@Service
public class IContentServiceImpl implements IContentService {

    @Autowired
    ContentConverter contentConverter;
    @Autowired
    PanelContentMapper panelContentMapper;
    /*
     * @Function:
     * 商品导航栏信息显示
     * @author yyr
     */
    @Override
    public NavListResponse queryNavList() {
        // 新建返回
        NavListResponse response = new NavListResponse();

        try {
            // 新建example 查询panelId为0的panelContent
            Example example = new Example(PanelContent.class);
            example.orderBy("sortOrder");
            example.createCriteria().andEqualTo("panelId", 0);
            // 进行查询
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            // 设置状态
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            // 设置数据
            response.setPanelContentDtos(contentConverter.panelContents2Dto(panelContents));
        } catch (Exception e) {
            // 打印异常信息
            log.error("IContentServiceImpl.queryNavList occur Exception:" + e);
            // 利用异常处理工具类处理异常
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        // 没有异常则返回数据
        return response;
    }
}
